﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WikiTrailCreator.Properties;
using System.Threading;

namespace WikiTrailCreator.WikiGeneration
{
    public partial class FormActivationCode : Form
    {
        private string trailName = "";

        public FormActivationCode()
        {
            InitializeComponent();
        }

        private void FormActivationCode_Load(object sender, EventArgs e)
        {
            string note = labelNote.Text;
            note= note.Replace("%EMAILADDRESS%", Settings.Default.WikiEmailAddress);
            labelNote.Text = note;
        }

        private long GenerateActivationCode(string userName, string trailName)
        {
            long userNameHash1 = Math.Abs(userName.GetHashCode() % 2521);
            long userNameHash2 = Math.Abs(userName.GetHashCode() % 1871);
            long userNameHash3 = Math.Abs(userName.GetHashCode() % 2069);
            long userNameHash4 = Math.Abs(userName.GetHashCode() % 2521);

            long trailNameHash1 = Math.Abs(trailName.GetHashCode() % 3391);
            long trailNameHash2 = Math.Abs(trailName.GetHashCode() % 2689);
            long trailNameHash3 = Math.Abs(trailName.GetHashCode() % 2663);
            long trailNameHash4 = Math.Abs(trailName.GetHashCode() % 3391);

            long result1 = userNameHash1 * trailNameHash4;
            long result2 = userNameHash2 * trailNameHash3;
            long result3 = userNameHash4 * trailNameHash1;
            long result4 = userNameHash3 * trailNameHash2;

            long finalResult = (result1 * (result2 % result3) * result4) % 34993499;

            return finalResult;
        }

        private void buttonActivate_Click(object sender, EventArgs e)
        {
            string realActCode = GenerateActivationCode(Settings.Default.WikiUserName, trailName).ToString();

            Thread.Sleep(1000);

            if (realActCode == textBoxActCode.Text)
            {
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show(this, "Sorry, this activation code is not valid.", "Verification Failed", MessageBoxButtons.OK);
            }
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        public string TrailName
        {
            set { trailName = value; }
        }
    }
}
